package com.myfoon.action.backing.context;


import com.myfoon.action.backing.system.UserBean;
import com.myfoon.model.entity.User;
import com.myfoon.model.entity.UserPi;

import javax.faces.context.FacesContext;

import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class LoginBean {
    private User user;        
    private boolean loggedIn;        
    private int timezone;
    
    public LoginBean() {
        loggedIn = false;        
        user = new User();
        user.setUserPi(new UserPi());
    }

    public void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }
    
    public String login() {                        
        UserBean userbean = new UserBean();
        
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Cookie cookie[] = ((HttpServletRequest)facesContext.getExternalContext().
        getRequest())
        .getCookies();
                        
        try {
            if(cookie != null && cookie.length > 0){
                  for(int i = 0; i<cookie.length; i++){
                      if (cookie[i].getName().equals("timezone")) {
                        this.timezone = Integer.valueOf(cookie[i].getValue())*-1;                 
                      }                      
                  }
            }   
        }        
        catch(Exception ex) {
            ;
        }
        
        
        try {
            user = userbean.findByEmailAndPassword(user.getEmail(), user.getPasswd());    
            //user.setUserPi(userbean.findUserDetails(user));
            
        }
        catch(EntityNotFoundException ex) {
            return "pretty:";
        }
        catch(NoResultException ex) {
            return "pretty:";
        }
        
        setLoggedIn(true);        
             
        return "pretty:home";                
    }
    
    public String loginiphone() {                        
        UserBean userbean = new UserBean();
        
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Cookie cookie[] = ((HttpServletRequest)facesContext.getExternalContext().
        getRequest())
        .getCookies();
                        
        try {
            if(cookie != null && cookie.length > 0){
                  for(int i = 0; i<cookie.length; i++){
                      if (cookie[i].getName().equals("timezone")) {
                        this.timezone = Integer.valueOf(cookie[i].getValue())*-1;                 
                      }                      
                  }
            }   
        }        
        catch(Exception ex) {
            ;
        }
        
        
        try {
            user = userbean.findByEmailAndPassword(user.getEmail(), user.getPasswd());    
            //user.setUserPi(userbean.findUserDetails(user));
            
        }
        catch(EntityNotFoundException ex) {
            return "pretty:";
        }
        catch(NoResultException ex) {
            return "pretty:";
        }
        
        setLoggedIn(true);        
             
        return "pretty:iphone_home";                
    }
        
    public void setUser(User user) {
        this.user = user;
    }

    public User getUser() {
        return user;
    }

    public void setTimezone(int timezone) {
        this.timezone = timezone;
    }

    public int getTimezone() {
        return timezone;
    }
}
